home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8996 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  85 lines

  1. Newsgroups: comp.lang.c
  2. Path: bcc.ac.uk!slidel
  3. From: slidel@bsm.bioc.ucl.ac.uk (Timothy Slidel)
  4. Subject: Re: do || die;
  5. Message-ID: <1996Mar7.212656.4757@ucl.ac.uk>
  6. Date: Thu, 7 Mar 1996 21:26:56 GMT
  7. References: <1996Mar7.052636.59812@ucl.ac.uk> <DnwJKs.KyJ@uns.bris.ac.uk>
  8. Organization: University College London
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Nathan Sidwell (nathan@pact.srf.ac.uk) wrote:
  12. : The expressions 'i && i--' looks like a simple boolean conditional.
  13. : However, your intent is the sideffect of i--. As such
  14. :     if(i)
  15. :         i--;
  16. : would be much clearer.
  17.  
  18. Yes, but once you grasp the do || die concept it seems almost natural, as
  19. in:
  20.  
  21.     "Give me the money _or_ I'll blow your head off!!"
  22.     "Give me the money _and_ you'll live"
  23.  
  24. (too much Duke Nukem I think!!)
  25.  
  26. What I'm trying to do is to extend two array markers outwards by one
  27. character without violating the array bounds, so I can do either:
  28.  
  29.     (s && s--) || (ary[e + 1] && e++);
  30.     (ary[e + 1] && e++) || (s && s--);
  31.  
  32. .. where the string array is terminated by '\0' ..
  33.  
  34. or..
  35.  
  36.     if (s)
  37.       s--;
  38.     else if (ary[e + 1])
  39.       e++;
  40.     if (ary[e + 1])
  41.       e++;
  42.     else if (s)
  43.       s--;
  44.  
  45. Oops, hang on - yes I think the first bit is ok provided if s == 1 then
  46. (s && s--) still evaluates to true, (s && --s) would not... (I'm starting
  47. to lose my own argument here!). Without the Perl do || die concept in mind
  48. then the second case is clearer but just seems a bit long winded - would
  49. there be any performance increase in the first case - say if you had this
  50. in a loop or something?
  51.  
  52. : Suppose I have a processing function which, if it fails I want to set an
  53. : error flag. What is the important thing here? setting the error flag
  54. : or processing the data? probably processing the data. Which of the following
  55. : two code fragments more clearly conveys what is happening?
  56. :
  57. :     if(processing_function())
  58. :         error = 1;
  59. : or
  60. :     error |= processing_function();
  61.  
  62. Ok - the second form here is not a good idea but:
  63.  
  64.     !processing_function() || (error = 1);
  65.  
  66. perhaps is - emphasises the function call.
  67.  
  68.     error = processing_function();
  69.  
  70. might be even better =:) What about:
  71.  
  72.     (s = fopen(blah...)) || error("Couldn't open ...");
  73.  
  74. ?
  75.  
  76. Are there any really bad things that can happen if you do this?
  77. Do any compilers produce an error? - is it portable?
  78.  
  79. --
  80. Tim Slidel,                                   Email: t.slidel@biochem.ucl.ac.uk
  81. BSM Unit, Biochemistry and Molecular Biology, Tel: +44 171419 3896
  82. University College, Gower Street,             Fax: +44 171380 7193
  83. London WC1E 6BT, United Kingdom.
  84.  
  85.